
var page = 1;
var phone = '',
    valid = '',
    provinceId = '',
    cityId = '',
    countyId = '',
    level = '';
var creatUser = '';
var ifRefer = true;//查询还是添加


function doAjax(){
    $.ajax({
        url:'/crm/agentC/search.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            pageNu:page,
            phone:phone,
            agencyTypeValue:level,
            valid:valid,
            provinceId:provinceId,
            cityId:cityId,
            countyId:countyId,
            salesmanUserName:creatUser,
        },
        type:'post',
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.datas.recordsTotal);
                $('#withdraw-record').empty();
                for(var i=0;i<data.datas.agentList.length;i++){
                    var ifValid = '';
                    if(data.datas.agentList[i].valid){
                        ifValid = '是';
                    }else{
                        ifValid = '否';
                    }
                    $('<tr class="gradeA odd">'+
                        '<a href=""><td class="sorting_1 withdrawId" style="text-decoration: underline;cursor: pointer;">'+data.datas.agentList[i].id+'</td></a>'+
                        '<td class=" ">'+data.datas.agentList[i].city+'</td>'+
                        '<td class=" ">'+data.datas.agentList[i].agencyType+'</td>'+
                        '<td class=" ">'+data.datas.agentList[i].name+'</td>'+
                        '<td class="sorting_1">'+data.datas.agentList[i].phone+'</td>'+
                        '<td class=" ">'+data.datas.agentList[i].agentPerformance+'</td>'+
                        '<td class=" ">'+data.datas.agentList[i].agentShopNumber+'</td>'+
                        '<td class=" ">'+data.datas.agentList[i].salesmanUserName+'</td>'+
                        '<td class="center ">'+ifValid+'</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }
                //id处理
                $('.withdrawId').click(function(){
                    var id = $(this).html();
                    window.location.href='/crm/shopCRMC/agentPerformance2List?agentId='+id
                })

                $(".pagination").createPage({
                    totalPage:data.datas.totalPage,
                    currPage:data.datas.pageNu,
                    pageCount:data.datas.pageNu,
                    turndown:'true',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });
            }
        }
    })
}

$(function(){
    doAjax();
})




//检验登录账号
$('#agencyuserName').click(function(){
    var username = $('#agencyloginName').val();
    checkUser(username);
})

//检验手机号
$('#agencyphone').blur(function(){
    var mobile = $('#agencyphone').val();
    checkMobile(mobile);
})

//查询
$('#refer').click(function(){
    phone = $('#phone').val();
    level = $('#level').val();
    valid = $('#state').val();
    provinceId = $('#address').val();
    cityId = $('#addressCity').val();
    countyId = $('#addressCounty').val();
    creatUser = $('#creatUser').val();
    page=1;
    doAjax();
})

//添加弹框加载省
$('#add').click(function(){
    ifRefer = false;
    doProvince();
})


//省
function doProvince(){
    $.ajax({
        url:'/crm/areaC/province.json',
        dataType:'json',
        data:{

        },
        type:'post',
        success:function(data){
            if(ifRefer){
                $('#address').empty();
                $('<option value="">请选择省</option>').appendTo($('#address'));
            }else{
                $('#chooseaddress').empty();
                $('<option value="">请选择省</option>').appendTo($('#chooseaddress'));
            }
            for(var i=0;i<data.datas.length;i++){
                var opStyle ='';
                //添加还是查询
                if(ifRefer){
                    $('<option class="provinceOption" value="'+data.datas[i].id+'">'+data.datas[i].provinceName+'</option>').appendTo($('#address'))
                }else{
                    //该区域是否可选
                    if(data.datas[i].hasUsed){
                        opStyle = 'background:#eee';
                        $('<option class="provinceOption" style="'+opStyle+'" disabled="disabled" value="'+data.datas[i].id+'">'+data.datas[i].provinceName+'</option>').appendTo($('#chooseaddress'))
                    }else{
                        //false可以点击
                        opStyle = 'background:#fff';
                        $('<option class="provinceOption" style="'+opStyle+'" value="'+data.datas[i].id+'">'+data.datas[i].provinceName+'</option>').appendTo($('#chooseaddress'))
                    }
                }
            }

            //查询省点击
            $('#address').change(function(){
                var optionId = $(this).val();
                doCity(optionId);
            })
            //添加省点击
            $('#chooseaddress').change(function(){
                var optionId = $(this).val();
                doCity(optionId);
            })
        }
    })
}

function doCity(optionId){
    $.ajax({
        url:'/crm/areaC/getCity.json',
        dataType:'json',
        data:{
            provinceId:optionId,
        },
        type:'post',
        success:function(data){
            if(ifRefer){
                $('#addressCity').empty();
                $('<option value="">请选择市</option>').appendTo($('#addressCity'));
            }else{
                $('#choosecity').empty();
                $('<option value="">请选择市</option>').appendTo($('#choosecity'));
            }
            for(var i=0;i<data.datas.length;i++){
                var opStyle ='';
                if(ifRefer){
                    $('<option class="cityOption" value="'+data.datas[i].id+'">'+data.datas[i].cityName+'</option>').appendTo($('#addressCity'))
                }else{
                    if(data.datas[i].hasUsed){
                        opStyle = 'background:#eee';
                        $('<option class="cityOption" style="'+opStyle+'" disabled="disabled" value="'+data.datas[i].id+'">'+data.datas[i].cityName+'</option>').appendTo($('#choosecity'))
                    }else{
                        //false可以点击
                        opStyle = 'background:#fff';
                        $('<option class="cityOption" style="'+opStyle+'" value="'+data.datas[i].id+'">'+data.datas[i].cityName+'</option>').appendTo($('#choosecity'))
                    }
                }
            }
            //市点击
            $('#addressCity').change(function(){
                var optionId2 = $(this).val();
                doCounty(optionId2);
            })
            //添加市点击
            $('#choosecity').change(function(){
                var optionId2 = $(this).val();
                doCounty(optionId2);
            })
        }
    })
}

function doCounty(optionId2){
    $.ajax({
        url:'/crm/areaC/getArea.json',
        dataType:'json',
        data:{
            cityId:optionId2,
        },
        type:'post',
        success:function(data){
            if(ifRefer){
                $('#addressCounty').empty();
                $('<option value="">请选择县/区</option>').appendTo($('#addressCounty'));
            }else{
                $('#chooseCounty').empty();
            }
            for(var i=0;i<data.datas.length;i++){
                var opStyle ='';
                if(ifRefer){
                    $('<option class="countyOption" value="'+data.datas[i].id+'">'+data.datas[i].areaName+'</option>').appendTo($('#addressCounty'))
                }else{
                    if(data.datas[i].hasUsed){
                        opStyle = 'background:#eee';
                        $('<label></label><input type="checkbox" name="county" disabled="disabled" value="'+data.datas[i].id+'" id="'+data.datas[i].id+'"><span style="margin-right: 20px;">'+data.datas[i].areaName+'</span>').appendTo($('#chooseCounty'))
                    }else{
                        //false可以点击
                        opStyle = 'background:#fff';
                        $('<label></label><input type="checkbox" name="county" value="'+data.datas[i].id+'" id="'+data.datas[i].id+'"><span style="margin-right: 20px;">'+data.datas[i].areaName+'</span>').appendTo($('#chooseCounty'))
                    }
                }
            }
        }
    })
}

doProvince();

//点击关闭
$('.close,#close').click(function(){
    ifRefer = true;
})

//区域id拼接
var s = '';
function chk(){
    var obj=document.getElementsByName('county');
    s='';
    for(var i=0; i<obj.length; i++){
        if(obj[i].checked) s+=obj[i].value+':';
    }

}

//获取代理提成比例
//$('#chooselevel').change(function(){
//    if($('#chooselevel').val()==1){
//        $('#pushMoney').val(0.07);
//    }else if($('#chooselevel').val()==2){
//        $('#pushMoney').val(0.06);
//    }else if($('#chooselevel').val()==3){
//        $('#pushMoney').val(0.05);
//    }else if($('#chooselevel').val()==4){
//        $('#pushMoney').val(0.05);
//    }
//})


//获取业务员
function doSalesMan(){
    $.ajax({
        url:'/crm/agentSalesmanC/getZongBuSalesman',
        dataType:'json',
        data:{},
        type:'post',
        success:function(data){
            for(var i=0;i<data.datas.length;i++){
               $('<option value="'+data.datas[i].id+'">'+data.datas[i].realName+'</option>').appendTo($('#salesmanId'))
            }
        }
    })
}
doSalesMan();

//点击保存
$('#save').click(function(){
    //拼接区域id
    chk();
    var agencyloginName = $('#agencyloginName').val();
    var agencyuserName = $('#agencyuserName').val();
    var agencyidentify = $('#agencyidentify').val();
    var agencypassword = $('#agencypassword').val();
    var agencyconpassword = $('#agencyconpassword').val();
    var agencyphone = $('#agencyphone').val();
    var chooselevel = $('#chooselevel').val();
    var softwareFee = $('#softwareFee').val();
    var serviceFee = $('#serviceFee').val();
    var chooseaddress = $('#chooseaddress').val();
    var choosecity = $('#choosecity').val();
    var pushMoney = $('#pushMoney').val();
    if(!checkUser(agencyloginName)){
        return;
    }
    if(!checkMobile(agencyphone)){
        return;
    }
    if(agencypassword!=agencyconpassword){
        alert('两次输入的密码不相同');
        return;
    }
    $.ajax({
        url:'/crm/agentC/toAdd.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            userName:agencyloginName,
            realName:agencyuserName,
            IDCode:agencyidentify,
            password:agencypassword,
            phone:agencyphone,
            agencyTypeValue:chooselevel,
            softMoney:softwareFee,
            technologyMoney:serviceFee,
            provinceId:chooseaddress,
            cityId:choosecity,
            countyId:s,
            bilv:pushMoney,
            salesmanId:$('#salesmanId').val(),
        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('添加成功');
                $('#myModal5').removeClass('fade in');
                $('#myModal5').addClass('fade');
                $('#myModal5').attr('aria-hidden',true);
                $('#myModal5').css('display','none');
                $('.modal-backdrop').css('display','none');
                $('#myModal5 input').val('');
                page=1;
                doAjax();
            }
        }
    })
})




